package minuy.android.slowcarremotecontrol.tool;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class SPHelper {
    private String defaultModelName;

    private static SPHelper spHelperInstant;

    private SPHelper() {

    }

    public static SPHelper getInstant(Context context) {
        if (spHelperInstant == null) {
            synchronized (SPHelper.class) {
                if (spHelperInstant == null) {
                    spHelperInstant = new SPHelper();
                    spHelperInstant.defaultModelName = "minuy_nlcould_app";
                }
            }
        }
        return spHelperInstant;
    }

    /**
     * sharepreference方式存储
     *
     * @param context   context
     * @param modelName XML的文件名，通常取模块名
     * @param key       存储在XML中的key
     * @param value     值
     */
    public void putData2SP(Context context, String modelName, String key, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(modelName, Context.MODE_PRIVATE); // 私有数据
        Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public void putData2SP(Context context, String key, String value) {
        putData2SP(context, defaultModelName, key, value);
    }

    public void putData2SP(Context context, String modelName, String key, boolean value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(modelName, Context.MODE_PRIVATE); // 私有数据
        Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public void putData2SP(Context context, String key, boolean value){
        putData2SP(context, defaultModelName, key, value);
    }

    public String getStringFromSP(Context context, String key,String def) {
        return getStringFromSP(context, defaultModelName, key, def);
    }

    public  String getStringFromSP(Context context, String modelName, String key,String de) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(modelName, Context.MODE_PRIVATE); // 私有数据
        return sharedPreferences.getString(key, de);
    }




    public Boolean getBooleanFromSP(Context context, String key,boolean def){
        return getBooleanFromSP(context,defaultModelName,key,def);
    }

    public Boolean getBooleanFromSP(Context context, String key){
        return getBooleanFromSP(context,defaultModelName,key);
    }


    public Boolean getBooleanFromSP(Context context, String modelName, String key){
        return getBooleanFromSP(context,modelName,key,false);
    }

    public Boolean getBooleanFromSP(Context context, String modelName, String key,boolean def) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(modelName, Context.MODE_PRIVATE); // 私有数据
        return sharedPreferences.getBoolean(key, def);
    }
}
